package cn.bitkit.assist.code;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import cn.bitkit.base.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

/**
 * @author changbo
 */
public class ClassHelper {

    public static void jsonToEntity(String json){
        jsonToEntity(json, 0);
    }

    public static void jsonToEntity(String json, int swagger){
        if(!JSON.isValid(json)){
            return ;
        }
        StringJoiner stringJoiner = new StringJoiner("\n");
        JSONObject jsonObject = JSONObject.parseObject(json);
        jsonObject.keySet().forEach( e -> {
            if(swagger == 2){
                stringJoiner.add("@ApiModelProperty(value = \""+e+"\")");
            }
            if(swagger == 3){
                stringJoiner.add("@Schema(name = \""+e+"\")");
            }
            stringJoiner.add("String "+e+";");
        });
        System.out.println(stringJoiner);
    }

    public static List<String> jsonField(String json){
        if(!JSON.isValid(json)){
            return List.of();
        }
        StringJoiner stringJoiner = new StringJoiner("\n");
        JSONObject jsonObject = JSONObject.parseObject(json);
        return new ArrayList<>(jsonObject.keySet());
    }

    public static void fieldCopyCode(List<String> fieldList){
        StringJoiner stringJoiner = new StringJoiner("\n");
        fieldList.forEach( e -> {
            stringJoiner.add("target.set"+ StringUtil.upperFirst(e) +"(source.get"+StringUtil.upperFirst(e) +"());");
        });
        System.out.println(stringJoiner);
    }

}
